Repository navigation
fix(session): accept each JWT algorithm that the JWKS key can verify - #593
Open
gjtorikian wants to merge 1 commit into
Open
gjtorikian wants to merge 1 commit into
gjtorikian wants to merge 1 commit into
Conversation
The session token verifier has no list of permitted algorithms now. After pull request #589, that list had only RS256 and ES256. Thus a different algorithm needed a new SDK release. The `kid` header selects the JWKS key. The algorithm must agree with it. ruby-jwt has the algorithms for the key type and verifies the signature. An unsigned token and a token with a MAC are never valid. `JWK_ALGORITHMS` stays as a public constant. The SDK does not read it. No public name, parameter, return type or failure reason constant changes.
Contributor
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge; no actionable issues were found. Summary
No actionable issues were found. Tests were reviewed but not run.
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On
main, the session token verifier accepts a token only if its algorithm is RS256 or ES256. Pull request #589 added ES256 toJWK_ALGORITHMSand added a private table of key types. No release has #589. This pull request removes the table, and the SDK does not readJWK_ALGORITHMSnow.The SDK now has no list of permitted algorithm names. The
kidheader selects the JWKS key. The type, the curve and thealgmember of that key give the permitted algorithms. Thus a different algorithm needs no new list of names in an SDK release. The SDK accepts a token of that algorithm only if ruby-jwt and OpenSSL have the algorithm for the key.How the SDK selects the key and the algorithm
decode_jwtgets the algorithm object for thealgheader fromJWT::JWA.findand givesJWT.decodethat one object and a block that finds the key. The block gets the JWKS keys that have thekidof the token. If a key has analgmember, that member must be equal to thealgheader.JWT::JWK.importselects the key class fromkty, and the SDK makes the key from the public members of that class (members, thenverify_key). For an EC key, the token must use the one algorithm of the curve (jwa). For an RSA key, the token must use aJWT::JWA::RsaorJWT::JWA::Psalgorithm. ruby-jwt then verifies the signature with OpenSSL and checks the claims.An unsigned token and a token with a MAC are never valid. ruby-jwt gives an algorithm of the class
JWT::JWA::NoneorJWT::JWA::Hmacfor such a token, and the SDK then givesJWT.decodean empty algorithm list.JWT.decodethen raisesJWT::IncorrectAlgorithmbefore the block looks for a key. The SDK also uses no key that is a String, and the key of a symmetric JWK is a String.Checks in SDK code
JWT::JWA::NoneorJWT::JWA::Hmacalgorithm, the SDK gives ruby-jwt an empty algorithm list. Cause: ruby-jwt accepts an unsigned token if the list has itsnonealgorithm, and it verifies a MAC with a String key. The check stops such a token before the SDK looks for a key, with the reasoninvalid_jwt_algorithm.algheader must be equal to the name of the algorithm, with the same letter case. Cause: ruby-jwt compares names without case, so it accepts a token withrs256for RS256.algmember of the key must be equal to thealgheader, andusemust be absent orsig. Cause: ruby-jwt gets the OpenSSL key from the SDK, so it does not read these members.key_algorithm?). Cause: ruby-jwt gives no algorithm for an RSA key that has noalgmember. Thus the SDK code has theJWT::JWA::RsaandJWT::JWA::Psclasses. This check also selects the key when two keys have the samekid.r || 0x00 || s, which has one more byte.algmember replaces the algorithm of the curve.verify_keyof a symmetric key is the secret, which is a String.NoMethodErrorfor a member that is not a String, andJWT::UnsupportedKeyTypefor aktythat it does not have.algname that is not UTF-8, the SDK gives ruby-jwt an empty algorithm list. Cause:JWT::EncodedToken.newandJWT::JWA.findraiseArgumentErrorfor them.JWT::JWK.importraisesEncodingErrorfor that name. The SDK then uses the key without that member.verify_jwtraisesJWT::DecodeErrorfor anOpenSSL::OpenSSLErrorand for anArgumentError. Cause:JWT.decoderaisesArgumentErrorfor a token that is a number or an Array, and it does not rescue each OpenSSL error.Algorithms
invalid_jwt_algorithm.No change
SessionManagerandSession, and the failure reason constants.JWK_ALGORITHMSstays a frozen Array with the same value as onmain. The SDK does not read it now, and its comments say so.critrejection, the check ofkid, and the dependencies (workos.gemspec,Gemfile.lock).kid(not more than one in 30 seconds).Behavior changes
algmember, or it has thatalgmember. Onmain:invalid_jwt_algorithm. Now: valid.main:invalid_jwt_algorithm. Now:invalid_jwt_signaturefor an incorrect signature,expired_jwtfor an expired token, andinvalid_jwtfor other causes. Examples of other causes: a differentalgmember of the key, an unknownkid, acritheader. Also a key of a different type or curve.kid, when the last JWKS request is older than 30 seconds. Onmain: no new JWKS request. Now: one new JWKS request, as for RS256 onmain.kid, and the first key is not correct. Examples: an EC point that is not on the curve, a member that is not Base64. Onmain:invalid_jwt. Now: the SDK ignores that key and uses the subsequent key.true, an Array, a Hash). Also annorxmember of a JWKS key that is not UTF-8. Onmain:authenticateanddecode_jwtraise anArgumentError. Now: the reason isinvalid_jwt, anddecode_jwtraisesJWT::DecodeError.decode_jwtwhen the key of the token is not correct. Onmain:JWT::JWKErrororJWT::Base64DecodeError. Now:JWT::DecodeError. The two classes are subclasses ofJWT::DecodeError.main:invalid_jwt_algorithm. Now: valid for a correct signature, if the key class has themembers,jwaandverify_keymethods. The tests of this pull request show this with a test key class and a test algorithm. A key class with nomembersmethod, or with a String key, givesinvalid_jwt.Tests
algmember and for a key with thatalgmember.session_manager.rbhas only the algorithm names RS256 and ES256, inJWK_ALGORITHMS. With that constant empty, only the two tests that compare its value fail.algheader that is not thealgmember of the key.none, and HS256, HS384 and HS512 with the public key as the secret. Also HS256, HS384 and HS512 with the secret of anoctkey that has thekidof the token.jwk,jku,x5corx5uheader member.ktyorcrv, anoctkey, a member that is not correct.mainhas a new name and new assertions, because it tests the list. It now asserts that the SDK accepts ES384. For a key withalgRS256, it assertsinvalid_jwtfor RS384, RS512 and PS256. A second test ofmaingets assertions forx5candx5u. The pull request removes no test.Documentation
Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.
If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.